<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>赵氏集团</title>
    <link rel="stylesheet" href="https://www.layuicdn.com/layui/css/layui.css" type="text/css" media="all">
</head>
<body>
<div style="margin-bottom: 5px;">

    <!-- 示例-970 -->
    <ins class="adsbygoogle" style="display:inline-block;width:970px;height:90px" data-ad-client="ca-pub-6111334333458862" data-ad-slot="3820120620"></ins>

</div>

<div class="layui-btn-group demoTable">
    <button class="layui-btn" data-type="getCheckData">获取选中行数据</button>
    <button class="layui-btn" data-type="getCheckLength">获取选中数目</button>
    <button class="layui-btn" data-type="isAll">验证是否全选</button>
</div>

<table class="layui-table" lay-data="{width: 892, height:330, url:'/spring-test', page:true,limit:2,limits:[2,4,6], id:'idTest'}" lay-filter="demo">
    <thead>
    <tr>
        <th lay-data="{type:'checkbox', fixed: 'left'}"></th>
        <th lay-data="{field:'id', width:80, sort: true, fixed: true}">ID</th>
        <th lay-data="{field:'username', width:80}">用户名</th>
        <th lay-data="{field:'sex', width:80, sort: true,templet:
			function(d){return d.sex=='1'?'男':'女'}}">性别</th>
        <th lay-data="{field:'password', width:80}">密码</th>
        <th lay-data="{fixed: 'right', width:178, align:'center', toolbar: '#barDemo'}"></th>
    </tr>
    </thead>
</table>

<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">查看</a>
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<!--点击后才显示-->
<div class="layui-row" id="popUpdateTest" style="display:none;">
    <div class="layui-col-md10">
        <form class="layui-form layui-from-pane" lay-filter="aaa" action="" style="margin-top:20px">
            <!--不加id，更新时找不到id，更新会失败, 添加hidden属性隐藏-->
            <div class="layui-form-item" hidden="true">
                <label class="layui-form-label">编号</label>
                <div class="layui-input-block">
                    <input type="text" name="id" placeholder="请输入编号" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">名字</label>
                <div class="layui-input-block">
                    <input type="text" name="username" required lay-verify="required" placeholder="请输入名字" autocomplete="off"
                           class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">性别</label>
                <div class="layui-input-block">
                    <input type="radio" name="sex" value="男" title="男">
                    <input type="radio" name="sex" value="女" title="女" checked>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">密码</label>
                <div class="layui-input-block">
                    <input type="text" name="password" required lay-verify="required" placeholder="请输入密码" autocomplete="off"
                           class="layui-input">
                </div>
            </div>

            <div class="layui-form-item" style="margin-top:40px">
                <div class="layui-input-block">
                    <button class="layui-btn  layui-btn-submit " lay-submit="" lay-filter="demo11">确认修改</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
        </form>
    </div>
</div>

<script src="http://libs.baidu.com/jquery/1.9.0/jquery.js" type="text/javascript"></script>
<script src="https://www.layuicdn.com/layui/layui.js" type="text/javascript"></script>
<script>
    layui.use(['table','jquery','form'], function(){
        var table = layui.table,$=layui.jquery,form=layui.form;

        /*layer.ready(function(){
            $.ajax({
                async: false,
                type: "POST",
                dataType: "json",
                url: '/spring-test',
                contentType: "application/x-www-form-urlencoded",
                data: {
                },
                success: function (result) {
                    alert(JSON.stringify(result));
                }
            });
        });*/
        //监听表格复选框选择
        table.on('checkbox(demo)', function(obj){
            console.log(obj)
        });
        //监听工具条
        table.on('tool(demo)', function(obj){
              var data = obj.data;
            if(obj.event === 'detail'){
                layer.msg('ID：'+ data.id + ' 的查看操作');
            } else if(obj.event === 'del'){
                layer.confirm('真的删除行么', function(index){
                    obj.del();
                    layer.close(index);
                });
            } else if(obj.event === 'edit'){
                    //layer.alert('编辑 [id]：' + data.id);
                    var data1 = data;
                    console.log("新data1为：", data1);
                    updateuser(obj, data1);
            }
        });

        var $ = layui.$, active = {
            getCheckData: function(){ //获取选中数据
                var checkStatus = table.checkStatus('idTest')
                    ,data = checkStatus.data;
                layer.alert(JSON.stringify(data));
            }
            ,getCheckLength: function(){ //获取选中数目
                var checkStatus = table.checkStatus('idTest')
                    ,data = checkStatus.data;
                layer.msg('选中了：'+ data.length + ' 个');
            }
            ,isAll: function(){ //验证是否全选
                var checkStatus = table.checkStatus('idTest');
                layer.msg(checkStatus.isAll ? '全选': '未全选')
            }
        };

        $('.demoTable .layui-btn').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        //编辑更新用户
        function updateuser(obj, data) {

            //回填数据到表单
            form.val("aaa", {
                "id": data.id
                , "username": data.username
                , "sex": data.sex =="0"?"女":"男"
                , "password": data.password
            });
            layer.open({
                //layer提供了5种层类型。可传入的值有：0（信息框，默认）1（页面层）2（iframe层）3（加载层）4（tips层）
                type: 1,
                title: "修改客户信息",
                area: ['420px', '330px'],
                content: $("#popUpdateTest")//引用的弹出层的页面层的方式加载修改界面表单
            });

            //console.log("修改原始数据为：", data);
            //console.log("id类型", typeof data.id);
            //console.log("obj为：", obj);
            form.on('submit(demo11)', function (massage) {

                //console.log("message为：", massage);
                //console.log(massage.field);
                var data1 = massage.field;
                massage.field.sex = massage.field.sex=="男"?"1":"0";
                $.ajax({
                    type: "post",
                    url: "/Tableupdate",
                    data: JSON.stringify(massage.field),
                    // data: { id: data.id, name: data1.name, sex: data1.sex, tell: data1.tell, addr: data1.addr },
                    contentType: 'application/json',
                    datatype: "json",
                    success: function (resp) {
                        console.log(resp);
                        layer.closeAll();//关闭所有的弹出层
                        if (resp == 1) {
                            layer.msg("修改成功", {icon: 6}, function () {
                                // 获得frame索引
                                var index = parent.layer.getFrameIndex(window.name);
                                //关闭当前frame
                                parent.layer.close(index);
                                //修改成功后刷新父界面
                                window.parent.location.reload();
                            });
                        } else {
                            layer.msg("修改失败", {icon: 5});
                        }
                    }
                })
                return false

            })
        }
    });
</script>
</body>
</html>